/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.losalpes.compras;

import com.losalpes.catalog.ICatalogService;
import com.losalpes.persistence.entity.Mueble;
import com.losalpes.security.SecurityBean;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

/**
 *
 * @author Oscar Ballesteros
 */

public class CatalogoVirtualBean {

    @EJB
    private ICatalogService catalogService;
    private boolean mostrarEditar;
    private List<Mueble> productos;
    private Mueble mueble;
    private SecurityBean securityBean;
    private CarritoBean carritoBean;

    /** Creates a new instance of CatalogoVirtualBean */
    public CatalogoVirtualBean() {
        
    }

    @PostConstruct
    public void obtenerCatalogoVirtual(){
        this.productos = catalogService.findAllMuebles();
    }

    public String agregarCarritoCompra(){
        carritoBean.agregarItemCarrito(mueble);
        return "go_carrito";
    }

    public void paint(OutputStream stream, Object object) throws IOException {
        System.out.println("pintando ");
        if (object != null) {
            stream.write((byte[])object);
        } else {
            FacesContext fc = FacesContext.getCurrentInstance();
            ServletContext sc = (ServletContext) fc.getExternalContext().getContext();
            InputStream is = sc.getResourceAsStream("img/empty.jpg");
            int lectura = 0;
            while ((lectura = is.available()) != 0) {
                byte[] leido = new byte[lectura];
                is.read(leido, 0, lectura);
                stream.write(leido);
            }
        }
    }

    public boolean isMostrarEditar() {
        return mostrarEditar;
    }

    public void setMostrarEditar(boolean mostrarEditar) {
        this.mostrarEditar = mostrarEditar;
    }

    public Mueble getMueble() {
        return mueble;
    }

    public void setMueble(Mueble mueble) {
        this.mueble = mueble;
    }

    public List<Mueble> getProductos() {
        return productos;
    }

    public void setProductos(List<Mueble> productos) {
        this.productos = productos;
    }

    public SecurityBean getSecurityBean() {
        return securityBean;
    }

    public void setSecurityBean(SecurityBean securityBean) {
        this.securityBean = securityBean;
    }

    public CarritoBean getCarritoBean() {
        return carritoBean;
    }

    public void setCarritoBean(CarritoBean carritoBean) {
        this.carritoBean = carritoBean;
    }
    

}
